import { Graphics, Point } from "pixi.js";

export class ConnectionLine extends Graphics {
    constructor(beginPoint: Point, endPoint: Point) {
        super();
        this._draw(beginPoint, endPoint);
        this.visible = false;
    }

    private _draw(beginPoint: Point, endPoint: Point) {
        //cal distance first;
        let dist = Math.sqrt(Math.pow((endPoint.x - beginPoint.x), 2) + Math.pow((endPoint.y - beginPoint.y), 2));

        this.lineStyle(2, 0xFFFFFF, 1);
        this.moveTo(0, 0);
        this.lineTo(dist, 0);

        this.lineTo(dist - 10, -10);
        this.moveTo(dist, 0);
        this.lineTo(dist - 10, 10);

        this.x = beginPoint.x;
        this.y = beginPoint.y;

        let rotation = Math.atan2((endPoint.y - beginPoint.y), (endPoint.x - beginPoint.x));
        this.rotation = rotation;
    }

    public updateBeginAndEndPoint(beginPoint: Point, endPoint: Point) {
        this.clear();
        this._draw(beginPoint, endPoint);
    }
}